package com.nengchuan.eic.nceconomy.util;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.security.SecureRandom;
import java.util.List;
import java.util.Random;
import java.util.UUID;

public class IdUtils {

    //获取格式化UUID
    public static String getFormatUUID(){
        UUID uuid=UUID.randomUUID();
        return uuid.toString().replaceAll("-","");
    }

    private static final int LENGTH = 13;

    private static void swap(int i, int j, int[] nums) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    /**
     * 采用随机数(1~1000)加上时间戳形成的序列
     * 适用于并发比较小的非分布式系统
     * @return
     */
    public static synchronized long  timestampRandomId(){
        long now=System.currentTimeMillis();
        Random random=new Random();
        int randNum=random.nextInt(1000)+1;
        String seq=randNum+""+now;
        return Long.parseLong(seq);
    }


    public static Long generateNumber() {
        String no = "";
        //初始化备选数组
        int[] defaultNums = new int[13];
        for (int i = 0; i < defaultNums.length; i++) {
            defaultNums[i] = i;
        }

        SecureRandom random = new SecureRandom();
        int[] nums = new int[LENGTH];
        //默认数组中可以选择的部分长度
        int canBeUsed = 13;

        //填充目标数组
        for (int i = 0; i < nums.length; i++) {
            //将随机选取的数字存入目标数组
            int index = random.nextInt(canBeUsed);
            nums[i] = defaultNums[index];

            //将已用过的数字扔到备选数组最后，并减小可选区域
            swap(index, canBeUsed - 1, defaultNums);
            canBeUsed--;
        }

        if (nums.length>0) {
            for (int i = 0; i < nums.length; i++) {
                no += nums[i];
            }
        }
        return Long.valueOf(no);
    }


    /*public static void main(String[] args) {
        System.out.println(generateNumber());
    }*/



    /**
     * description:判断当前对象是否为空（包括所有属性为空）
     * 可选则在判断规则中剔除某一字段，或者某一值
     * @author jiangqingpeng
     * @param object 入参对象
     * @param excludeNameList 要剔除的属性名称,没有就传空集合或者null
     * @param excludeValueList 要剔除的数值,没有就传空集合或者null
     * @return boolean
     *  在判断中剔除 value 为男性的属性
        objCheckIsNull(user,null, Collections.singletonList("男性"));
        //在判断中剔除 属性名 为 sex, childSex 的属性
        CommonFunction.objCheckIsNull(user, Arrays.asList("sex","childSex"), null);
     *  @create 2020-04-01
     **/
    public static boolean objCheckIsNull(Object object, List<String> excludeNameList, List<Object> excludeValueList) {
        if (object == null) {
            return true;
        }
        // 得到类对象
        Class clazz = object.getClass();
        // 得到所有属性
        Field[] fields = clazz.getDeclaredFields();
        //判断入参
        boolean excludeNameListFlag = false;
        if (excludeNameList != null && excludeNameList.size() > 0) {
            excludeNameListFlag = true;
        }
        boolean excludeValueListFlag = false;
        if (excludeValueList != null && excludeValueList.size() > 0) {
            excludeValueListFlag = true;
        }
        //定义返回结果，默认为true
        boolean flag = true;
        for (Field field : fields) {
            field.setAccessible(true);
            Object fieldValue = null;
            String fieldName = null;
            try {
                //得到属性值
                fieldValue = field.get(object);
                //得到属性类型
                Type fieldType = field.getGenericType();
                //得到属性名
                fieldName = field.getName();
                //剔除指定属性名的属性值
                if (excludeNameListFlag) {
                    for (String s : excludeNameList) {
                        if (fieldName.equals(s)) {
                            fieldValue = null;
                        }
                    }
                }
                //剔除指定属性值
                if (excludeValueListFlag) {
                    for (Object obj : excludeValueList) {
                        if (fieldValue == obj) {
                            fieldValue = null;
                        }
                    }
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
            //只要有一个属性值不为null 就返回false 表示对象不为null 忽略序列化
            if (fieldValue != null && !"serialVersionUID".equals(fieldName)) {
                flag = false;
                break;
            }
        }
        return flag;
    }

}
